package cn.gcyl.module.biz.service.useractionlog;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gcyl.module.biz.controller.admin.useractionlog.vo.*;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.useractionlog.UserActionLogMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.USER_ACTION_LOG_NOT_EXISTS;

/**
 * 用户操作日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserActionLogServiceImpl implements UserActionLogService {

    @Resource
    private UserActionLogMapper userActionLogMapper;

    @Override
    public Long createUserActionLog(UserActionLogSaveReqVO createReqVO) {
        // 插入
        UserActionLogDO userActionLog = BeanUtils.toBean(createReqVO, UserActionLogDO.class);
        userActionLogMapper.insert(userActionLog);

        // 返回
        return userActionLog.getId();
    }

    @Override
    public void updateUserActionLog(UserActionLogSaveReqVO updateReqVO) {
        // 校验存在
        validateUserActionLogExists(updateReqVO.getId());
        // 更新
        UserActionLogDO updateObj = BeanUtils.toBean(updateReqVO, UserActionLogDO.class);
        userActionLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserActionLog(Long id) {
        // 校验存在
        validateUserActionLogExists(id);
        // 删除
        userActionLogMapper.deleteById(id);
    }

    @Override
        public void deleteUserActionLogListByIds(List<Long> ids) {
        // 删除
        userActionLogMapper.deleteByIds(ids);
        }


    private void validateUserActionLogExists(Long id) {
        if (userActionLogMapper.selectById(id) == null) {
            throw exception(USER_ACTION_LOG_NOT_EXISTS);
        }
    }

    @Override
    public UserActionLogDO getUserActionLog(Long id) {
        return userActionLogMapper.selectById(id);
    }

    @Override
    public PageResult<UserActionLogDO> getUserActionLogPage(UserActionLogPageReqVO pageReqVO) {
        return userActionLogMapper.selectPage(pageReqVO);
    }

}